package com.tlscpro.zwt.controller;

import com.tlscpro.zwt.service.SystemUserService;
import com.tlscpro.zwt.entity.TlscUser;
import com.tlscpro.zwt.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/systemUser/")
public class SystemUserController {
    @Autowired
    SystemUserService systemUserService;

    SystemUserController(){}

    //查询全部用户
    @RequestMapping("selectSystemUserList")
    @ResponseBody
    public List<TlscUser> selectSystemUserList() {
        return systemUserService.selectSystemUserList();
    }

    //查询单个用户
    @RequestMapping("login")
    @ResponseBody
    public String selectSystemUser(@RequestParam String userName, @RequestParam String password,
                                   HttpServletRequest request, HttpServletResponse response){
        TlscUser tlscUser = systemUserService.selectSystemUser(userName, password);
        UUID uuid = UUID.randomUUID();
        String token = uuid.toString();
        //返回客户端token
        response.setHeader("token", token);
        //服务器保存一份token
        HttpSession session=request.getSession();
        session.setAttribute("token", token);
        //设置对话过期时间为30分钟
        session.setMaxInactiveInterval(1000*60*30);
        if (StringUtil.isNullOrEmpty(tlscUser))
            return "error";
        else
            return "success";
    }

    //添加一个用户
    @RequestMapping("insertSystemUser")
    @ResponseBody
    public int insertSystemUser(@RequestBody TlscUser tlscUser) {
        return systemUserService.insertSystemUser(tlscUser);
    }
}
